<?php
if (!file_exists(__DIR__ . '/settings.json') || !file_exists(__DIR__ . '/db.php')) {
    header("Location: install.php");
    exit;
}
require 'db.php';

$settings_file = __DIR__ . '/settings.json';
$settings = file_exists($settings_file) ? json_decode(file_get_contents($settings_file), true) : ['title' => '私人网盘'];
$site_title = $settings['title'];
$site_domain = $settings['site_domain'];

// 分页参数
$per_page = 6; // 每页显示 10 条
$page = isset($_GET['page']) && is_numeric($_GET['page']) ? (int)$_GET['page'] : 1;
$offset = ($page - 1) * $per_page;

// 获取总文件数
$total_stmt = $conn->query("SELECT COUNT(*) as total FROM files");
$total_files = $total_stmt->fetch_assoc()['total'];
$total_pages = ceil($total_files / $per_page);

// 获取当前页文件
$sql = "SELECT f.id, f.name, f.upload_time, u.username 
        FROM files f 
        JOIN users u ON f.user_id = u.id 
        ORDER BY f.upload_time DESC 
        LIMIT ? OFFSET ?";
$stmt = $conn->prepare($sql);
$stmt->bind_param("ii", $per_page, $offset);
$stmt->execute();
$result = $stmt->get_result();
?>

<!DOCTYPE html>
<html lang="zh-CN">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title><?= htmlspecialchars($site_title) ?></title>
    <link href="/assets/bootstrap/css/bootstrap.min.css" rel="stylesheet">
</head>
<body class="<?= $settings['theme'] === 'dark' ? 'bg-dark text-white' : '' ?>">
    <nav class="navbar navbar-expand-lg <?= $settings['theme'] === 'dark' ? 'navbar-dark bg-dark' : 'navbar-light bg-light' ?>">
        <div class="container">
            <a class="navbar-brand" href="index.php"><?= htmlspecialchars($site_title) ?></a>
            <button class="navbar-toggler" type="button" data-bs-toggle="collapse" data-bs-target="#navbarNav" aria-controls="navbarNav" aria-expanded="false" aria-label="Toggle navigation">
                <span class="navbar-toggler-icon"></span>
            </button>
            <div class="collapse navbar-collapse" id="navbarNav">
                <ul class="navbar-nav ms-auto">
                    <?php if (isset($_SESSION['user_id'])): ?>
                        <li class="nav-item"><a class="nav-link" href="index.php">首页</a></li>
                        <li class="nav-item"><a class="nav-link" href="upload.php">上传文件</a></li>
                        <li class="nav-item"><a class="nav-link" href="user_center.php">用户中心</a></li>
                        <?php if ($_SESSION['role'] === 'admin'): ?>
                            <li class="nav-item"><a class="nav-link" href="admin/index.php">后台管理</a></li>
                        <?php endif; ?>
                        <li class="nav-item"><a class="nav-link" href="logout.php">退出</a></li>
                    <?php else: ?>
                        <li class="nav-item"><a class="nav-link" href="login.php">登录</a></li>
                        <?php if ($settings['registration_enabled']): ?>
                            <li class="nav-item"><a class="nav-link" href="register.php">注册</a></li>
                        <?php endif; ?>
                    <?php endif; ?>
                </ul>
            </div>
        </div>
    </nav>

    <main class="container py-5">
        <?php if (!empty($settings['announcement'])): ?>
            <div class="alert alert-info"><?= htmlspecialchars($settings['announcement']) ?></div>
        <?php endif; ?>
        <?php if (isset($_GET['upload']) && $_GET['upload'] === 'success'): ?>
            <div class="alert alert-success">上传成功！</div>
        <?php endif; ?>
        <h2>文件列表</h2>
        <table class="table table-striped">
            <thead>
                <tr>
                    <th>序号</th>
                    <th>名称</th>
                    <th>上传时间</th>
                    <th>作者</th>
                    <th>操作</th>
                </tr>
            </thead>
            <tbody>
                <?php $index = $offset + 1; while ($file = $result->fetch_assoc()): ?>
                    <tr>
                        <td><?= $index++ ?></td>
                        <td><?= htmlspecialchars($file['name']) ?></td>
                        <td><?= $file['upload_time'] ?></td>
                        <td><?= htmlspecialchars($file['username']) ?></td>
                        <td><a href="download.php?id=<?= $file['id'] ?>" class="btn btn-primary btn-sm">下载</a></td>
                    </tr>
                <?php endwhile; ?>
            </tbody>
        </table>

        <!-- 分页导航 -->
        <?php if ($total_pages > 1): ?>
            <nav aria-label="Page navigation">
                <ul class="pagination justify-content-center">
                    <li class="page-item <?= $page <= 1 ? 'disabled' : '' ?>">
                        <a class="page-link" href="index.php?page=<?= $page - 1 ?>" aria-label="Previous">
                            <span aria-hidden="true">&laquo;</span>
                        </a>
                    </li>
                    <?php for ($i = 1; $i <= $total_pages; $i++): ?>
                        <li class="page-item <?= $i == $page ? 'active' : '' ?>">
                            <a class="page-link" href="index.php?page=<?= $i ?>"><?= $i ?></a>
                        </li>
                    <?php endfor; ?>
                    <li class="page-item <?= $page >= $total_pages ? 'disabled' : '' ?>">
                        <a class="page-link" href="index.php?page=<?= $page + 1 ?>" aria-label="Next">
                            <span aria-hidden="true">&raquo;</span>
                        </a>
                    </li>
                </ul>
            </nav>
        <?php endif; ?>
    </main>

    <footer class="bg-dark text-white text-center py-3">
        <p>© 2025 <?= htmlspecialchars($site_title) ?></p>
    </footer>

    <script src="/assets/bootstrap/js/bootstrap.bundle.min.js"></script>
</body>
</html>